﻿using RsCms.Core;
using RsCode;

namespace Plugin.Archives.Service
{
 
	public class ArchiveDomainService
	{
		IPostsRepository repository;
		IHttpContextAccessor httpContextAccessor;
		public ArchiveDomainService(IPostsRepository repository,
			IHttpContextAccessor httpContextAccessor)
		{
			this.repository = repository;
			this.httpContextAccessor = httpContextAccessor;
		}

		string LoginUserId
		{
			get
			{
				var claims = httpContextAccessor?.HttpContext?.User.Claims;
				return claims?.First(x => x.Type == "UserId").Value ?? "";
			}
		}
		bool IsAdmin
		{
			get
			{
				var claims = httpContextAccessor?.HttpContext?.User.Claims;
				var role = claims?.First(x => x.Type == "Role").Value ?? "";
				if (!string.IsNullOrWhiteSpace(role))
				{
					return Convert.ToInt32(role) == 2 ? true : false;
				}
				return false;
			}
		}

		public async Task<RsCms.Entities.Posts> GetArchiveAsync(string IdOrKey)
		{
			var info = await repository.PostStore.Select.Where(x => (x.Id == IdOrKey || x.Title.Contains(IdOrKey) && x.Type == "archive")).ToOneAsync();
			return info;
		}
		public async Task<PageData<RsCms.Entities.Posts>> PageArchiveAsync(int page, int pageSize, string[] date = null)
		{
			if (!IsAdmin)
			{
				return await PageArchiveByUserAsync(page, pageSize, date);
			}
			if (pageSize > 30) pageSize = 30;

			if (date == null || date[0]=="null")
			{
				var select = repository.PostStore.Select.Where(x => x.Type == "archive").OrderByDescending(x => x.CreatedAt);
				var info = await repository.PostStore.PageAsync(page, pageSize, select);
				return info;
			}
			else
			{
				try
				{
					DateTime startDate = DateTimeHelper.GetStartDateTime(Convert.ToDateTime(date[0]));
					DateTime endDate = DateTimeHelper.GetEndDateTime(Convert.ToDateTime(date[1]));

					var select = repository.PostStore.Select.Where(x => x.Type == "archive" && (x.CreatedAt >= startDate && x.CreatedAt <= endDate)).OrderByDescending(x => x.CreatedAt);
					return await repository.PostStore.PageAsync(page, pageSize, select);
				}
				catch (Exception)
				{

					return new PageData<RsCms.Entities.Posts> { };
				}

			}

		}

		public async Task<PageData<RsCms.Entities.Posts>> PageArchiveByUserAsync(int page, int pageSize, string[] date = null)
		{
			if (pageSize > 30) pageSize = 30;

			if (date == null)
			{
				var select = repository.PostStore.Select.Where(x => x.Type == "archive" && x.Visibility == "public" && x.Uuid == LoginUserId).OrderByDescending(x => x.CreatedAt);
				var info = await repository.PostStore.PageAsync(page, pageSize, select);
				return info;
			}
			else
			{
				try
				{
					DateTime startDate = DateTimeHelper.GetStartDateTime(Convert.ToDateTime(date[0]));
					DateTime endDate = DateTimeHelper.GetEndDateTime(Convert.ToDateTime(date[1]));

					var select = repository.PostStore.Select.Where(x => x.Type == "archive" && x.Visibility == "public" && (x.CreatedAt >= startDate && x.CreatedAt <= endDate) && x.Uuid == LoginUserId).OrderByDescending(x => x.CreatedAt);
					return await repository.PostStore.PageAsync(page, pageSize, select);
				}
				catch (Exception)
				{

					return new PageData<RsCms.Entities.Posts> { };
				}

			}

		}

		public async Task DeleteArchiveAsync(string postId)
		{
			var info = await GetArchiveAsync(postId);
			if (info != null)
			{
				repository.PostStore.Delete(info);
			}
		}
	}
}
